package ro.ubbcluj.cs.damate.services;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ro.ubbcluj.cs.damate.persistence.entities.Group;
import ro.ubbcluj.cs.damate.persistence.repository.GroupRepository;

import java.util.List;

/**
 * @author Lóránd Fazakas (lorand)
 *         Date: 5/10/12
 *         Time: 9:49 PM
 */
@Service
public class GroupService {

    @Autowired
    private GroupRepository groupRepository;

    @Transactional
    public void create(Group transientGroup) {
        groupRepository.save(transientGroup);
    }

    @Transactional
    public void update(Group persistentGroup) {
        groupRepository.save(persistentGroup);
    }

    @Transactional
    public void delete(Group persistentGroup) {
        groupRepository.delete(persistentGroup);
    }

    public List<Group> findAll() {
        return groupRepository.findAll();
    }

    public Group findGroupByName(String groupName) {
        List<Group> groups = groupRepository.findByGroupName(groupName);
        return (groups != null && !groups.isEmpty()) ? groups.get(0) : null;
    }
}
